#! /usr/bin/env/python3
# _*_ coding:utf-8 _*_
'''
    练习爬虫：获取一张图片 http://www.win4000.com/meitu.html
    作者：王楠
    日期：2020/03/05
    功能：
    1.0：获取图片url存到list    request   bs4
    2.0: 下载图片到本地    os  tkinter
'''
import requests as re
from bs4 import BeautifulSoup as bs
import tkinter as tk
from tkinter.filedialog import askdirectory
import urllib.request
import os


def get_html_soup(url):
    '''
    函数：根据网页返回soup对象
    '''
    r = re.get(url, timeout=30)
    s = bs(r.text, 'lxml')
    return s


def get_url_dict():
    '''
    获取所有meinvtag4页面上，每一个图片集的地址
    '''
    name_meitutag = 'meinvtag4'
    count = 0  # 记录图集个数
    url_dict = dict()  # 存放每一个图片集名称和地址
    print('开始获取meinvtag4图集地址：')
    for i in range(1, 6):  # 共5个页面
        url = 'http://www.win4000.com/' + name_meitutag + '_' + str(
            i) + '.html'
        s = get_html_soup(url)
        div_list_s = s.find('div', class_='Left_bar')
        for li_list_s in div_list_s.find_all('li'):
            # print(li_list_s)
            url_values = li_list_s.find('a')['href']
            url_name = li_list_s.find('img')['title']
            # print(url_name)
            # print(url_values)
            url_dict[url_name] = url_values  # 把图集名称和地址存进字典
            # rint('***************')
            count += 1  # 计数器加一
            if count % 5 == 0:
                print('正在获取第{}个图集...'.format(count))
    print('图集地址获取完毕，共获取到了{}个图集地址。'.format(count))
    # for name, url in url_dict.items():
    #     print(name+' : '+url)
    return url_dict


def get_image_url(name, url):
    '''
    获取每一个图集的图片地址
    '''
    url_list = []  # 存放一个图集的图片地址
    s = get_html_soup(url)
    max_num_of_image = int(s.find('em').text)
    # print(name + '  该图集共有{}张图：'.format(max_num_of_image))
    image_url_s = s.find('img', class_='pic-large')
    # print(image_url_s)
    url_list.append(image_url_s['data-original'])  # 第一张图的地址
    for i in range(2, max_num_of_image + 1):
        url_n = url[:-5] + '_' + str(i) + '.html'
        s = get_html_soup(url_n)
        image_url_s = s.find('img', class_='pic-large')
        url_list.append(image_url_s['data-original'])
    return url_list


def down_save_image(filepath, all_images_dict):
    '''
    根据图集名称，以及图集的图片地址下载图片
    '''
    def callbackfunc(blocknum, blocksize, totalsize):
        '''
            回调函数
            @blocknum: 已经下载的数据块
            @blocksize: 数据块的大小
            @totalsize: 远程文件的大小
        '''
        percent = 100.0 * blocknum * blocksize / totalsize
        if percent > 100:
            percent = 100
            if percent % 20 == 0 and percent <= 100:
                print('图片已经下载{}%'.format(percent))
    num = 1
    for name, images_url_list in all_images_dict.items():
        if os.path.exists(filepath + '/' + name) == False:
            os.mkdir(filepath + '/' + name)
        count = 1
        for image_url in images_url_list:
            print('正在下载第{}个图片集：<'+name+'>的第{}张图片'.format(num, count))
            result = urllib.request.urlretrieve(image_url,
                                                filename=filepath + '/' + name + '/' +
                                                str(count) + '.jpg',
                                                reporthook=callbackfunc,
                                                data=None)
            print('第{}个作品集：<'+name+'>的第{}张图片以保存在：'.format(num, count)+result[0])
            count += 1
        num += 1
    print('××××××××××××××××××××××××××××××××××××')
    print('下载完成,共下载{}张图。'.format(num*count))


def get_filepath():
    '''
    获取文件存放地址
    '''
    print('选择文件保存位置')
    window = tk.Tk()
    window.withdraw()
    filepath = askdirectory(title=u'选择文件')
    # print(filepath)
    return filepath


def main():
    '''
    主函数
    '''
    url_dict = get_url_dict()    # 获取图片集的地址
    all_images_dict = dict()     # 存放所有图片的地址
    count = 0  # 记录进度
    nums = 0  # 总图数量
    for name, url in url_dict.items():
        image_all_list = get_image_url(name, url)
        all_images_dict[name] = image_all_list
        count += 1
        print('第{}个图集的图片地址获取完成，该图集共有{}张图'.format(count, len(image_all_list)))
        nums += len(image_all_list)
    print('图片地址获取完毕,共获取图集{}个，含图片{}张'.format(count, nums))
    print(all_images_dict)
    filepath = get_filepath()
    down_save_image(filepath, all_images_dict)


if __name__ == '__main__':
    main()
